package com.sanadc.shohdi;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;


public class SampleTextFrame extends JFrame
{
	
	protected FontPropertiesPanel propertiesPanel;
	protected JTextField sampleText;
	protected JLabel displayArea;
	
	public static void main(String[] args)
	{
		SampleTextFrame stf = new  SampleTextFrame();
		stf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		stf.setVisible(true);
	}
	
	
	public SampleTextFrame()
	{
		super();
		createComponents();
		createDocumentListner();
		buildLayout();
		refreshDisplayFont();
		pack();
	}
	
	
	protected void createComponents()
	{
		propertiesPanel = new  FontPropertiesPanel(this);
		sampleText = new JTextField(20);
		displayArea = new JLabel("");
		displayArea.setPreferredSize(new Dimension(200,75));
		displayArea.setMinimumSize(new Dimension(200,75));
		
	}
	
	
	protected void createDocumentListner()
	{
		Document document = sampleText.getDocument();
		document.addDocumentListener(new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				handleDocumentUpdate();
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				handleDocumentUpdate();
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				handleDocumentUpdate();
			}
		});
		
		
	
	}
	
	protected void buildLayout()
	{
		Container pane = getContentPane();
		GridBagConstraints gbc = new  GridBagConstraints();
		GridBagLayout gbl = new GridBagLayout();
		pane.setLayout(gbl);
		
		gbc.insets = new Insets(5, 10, 5, 10);
		gbc.fill  = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1;
		gbc.gridx = 0;
		BevelBorder bb = new BevelBorder(BevelBorder.RAISED);
		TitledBorder tb = new TitledBorder (bb,"Font");
		propertiesPanel.setBorder(tb);
		gbl.setConstraints(propertiesPanel, gbc);
		pane.add(propertiesPanel);
		gbl.setConstraints(sampleText, gbc);
		pane.add(sampleText);
		gbl.setConstraints(displayArea, gbc);
		pane.add(displayArea);
		
	}
	
	protected void handleDocumentUpdate()
	{
		displayArea.setText(sampleText.getText());
	}
	
	
	
	
	
	public void refreshDisplayFont()
	{
		displayArea.setFont(getSelectedFont());
	}
	
	
	
	public Font getSelectedFont()
	{
		String name = propertiesPanel.getSelectedFontName();
		int style = 0;
		style += (propertiesPanel.isBoldSelected()?Font.BOLD:0);
		style += (propertiesPanel.isItalicSelected()?Font.ITALIC:0);
		int size = propertiesPanel.getSelectedFontSize();
		return new Font(name,style,size);
	}
}